package zy.blue7.flowable.idm;

import org.flowable.common.engine.api.management.TableMetaData;
import org.flowable.common.engine.api.management.TablePage;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.IdmIdentityService;
import org.flowable.idm.api.IdmManagementService;
import org.flowable.idm.api.User;
import org.flowable.idm.engine.IdmEngine;
import org.flowable.idm.engine.IdmEngineConfiguration;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class IdmTest {

    IdmEngine idmEngine;
    IdmIdentityService idmIdentityService;
    IdmEngineConfiguration idmEngineConfiguration;
    IdmManagementService idmManagementService;

    @Before
    public void init() {
        InputStream inputStream = IdmTest.class.getClassLoader().getResourceAsStream("flowable.idm.cfg.xml");
        idmEngine = IdmEngineConfiguration.createIdmEngineConfigurationFromInputStream(inputStream).buildIdmEngine();

        idmIdentityService = idmEngine.getIdmIdentityService();
        idmManagementService = idmEngine.getIdmManagementService();
        idmEngineConfiguration = idmEngine.getIdmEngineConfiguration();

        String name = idmEngine.getName();
        System.out.println("引擎的名称是 ： " + name);

    }

    @Test
    public void testInit() {
        System.out.println("--------");
    }

    @Test
    public void testAddUser() {
        UserEntityImpl user = new UserEntityImpl();
        user.setEmail("aa@qq.com");
        user.setId("001");
        user.setPassword("1111");
        //0代表新增 默认为1 会报空指针异常
        user.setRevision(0);
        idmIdentityService.saveUser(user);
    }
    @Test
    public void testUpdateUser() {
        UserEntityImpl user = new UserEntityImpl();
        user.setEmail("aa33@qq.com");
        user.setId("001");
        user.setPassword("1111--00");
        //0代表新增 默认为1 会报空指针异常
//        user.setRevision(0);
        idmIdentityService.updateUserPassword(user);
    }


    @Test
    public void testAddGroup() {
        GroupEntityImpl group = new GroupEntityImpl();
        group.setId("yanfanbu");
        group.setName("yanfa");
        //0代表新增 默认为1 会报空指针异常
        group.setRevision(0);
        idmIdentityService.saveGroup(group);
    }


    @Test
    public void testMemberShip1() {
        String userId = "000";
        String groupId = "yanfanbu";
        idmIdentityService.createMembership(userId, groupId);
    }

    @Test
    public void testMemberShip2() {
        String userId = "000";
        String groupId = "yanfanbu";
        idmIdentityService.createMembership(userId, groupId);
    }


    @Test
    public void testQueryGroup() {
        List<Group> list = idmIdentityService.createGroupQuery().list();

        for (Group group : list) {
            System.out.println(group.getId());
            System.out.println(group.toString());
        }
    }

    @Test
    public void testQueryUser() {
        List<User> list = idmIdentityService.createUserQuery().list();
        for (User user : list) {
            System.out.println(user.getId());
            System.out.println(user.toString());
        }
    }


    /**
     * 获取表信息
     */
    @Test
    public void testGetTableCount() {
        Map<String, Long> tableCount = idmManagementService.getTableCount();
        /**
         * long 表示是数据库有多少条数据
         *
         */
        Set<Map.Entry<String, Long>> entries = tableCount.entrySet();
        for (Map.Entry<String, Long> entry : entries) {
            System.out.println(entry.getKey() + "  " + entry.getValue());
        }
    }

    /**
     * 通过 class获取表名
     */
    @Test
    public void testTableName() {
        String tableName = idmManagementService.getTableName(User.class);
        System.out.println("table" + " " + tableName);
        String groupTableName = idmManagementService.getTableName(Group.class);
        System.out.println("table" + " " + groupTableName);
    }

    /**
     * 获取表的元数据
     */
    @Test
    public void testTableMetaData() {
        TableMetaData tableMetaData = idmManagementService.getTableMetaData("ACT_ID_USER");
        System.out.println(tableMetaData.toString());
        /**
         * 表的列
         */
        System.out.println(tableMetaData.getColumnNames());
        /**
         * 列的类型
         */
        System.out.println(tableMetaData.getColumnTypes());
        /**
         * 表名
         */
        System.out.println(tableMetaData.getTableName());
        System.out.println(tableMetaData.getClass());
    }

    @Test
    public void testTableProperties() {
        Map<String, String> properties = idmManagementService.getProperties();
        Set<Map.Entry<String, String>> entries = properties.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry.getKey() + "  " + entry.getValue());
        }
    }


    /**
     * 分页查询
     */
    @Test
    public void testTablePageQuery() {
        TablePage tablePage = idmManagementService.createTablePageQuery()
                .tableName("ACT_ID_USER")
                .listPage(1, 4);

        System.out.println(tablePage.getFirstResult());
        System.out.println(tablePage.getRows());
        System.out.println(tablePage.getSize());
        System.out.println(tablePage.getTableName());
        System.out.println(tablePage.getTotal());
    }
}
